# -*- coding: utf-8 -*-
from flask_login import UserMixin
from apps.ext import db
from datetime import datetime


class Kind(db.Model):
    """
      种类表
    """
    __tablename__ = 'kind'
    # 新闻种类ID
    type_id = db.Column(db.Integer, primary_key=True)
    type = db.Column(db.String(64), nullable=False)


class News(db.Model):
    """
      新闻详情表
    """
    __tablename__ = 'news'
    # 哈希新闻id
    hash_id = db.Column(db.String(64), primary_key=True)
    # 新闻标题
    title = db.Column(db.String(255), nullable=False, index=True)
    # 新闻内容
    content = db.Column(db.Text(), nullable=False)
    # 新闻分类
    type_id = db.Column(db.Integer, db.ForeignKey(Kind.type_id), index=True)
    # 图片地址
    img_url = db.Column(db.String(255))
    # 责任编辑
    author = db.Column(db.String(64), nullable=False)
    # 新闻来源
    news_from = db.Column(db.String(64), nullable=False)
    # 发布时间
    create_time = db.Column(db.DateTime, default=datetime.now)
    # 收藏人数
    store_up_num = db.Column(db.Integer, default=0)
    # 评论条数
    comment_num = db.Column(db.Integer, default=0)
    # 点击量
    click_num = db.Column(db.Integer, default=0)
    # 新闻内容反向引用
    # content = db.relationship('Text', uselist=True, lazy='dynamic')


# class Text(db.Model):
#     __tablename__ = "new_text"
#     # 文本ID
#     id = db.Column(db.Integer, primary_key=True)
#     # 新闻ID外键
#     hash_id = db.Column(db.String(64), db.ForeignKey(News.hash_id), index=True)
#     # 新闻内容
#     content = db.Column(db.Text(), nullable=False)
#     # 文本类型
#     text_type = db.Column(db.String(64), nullable=False)

# 收藏表(多对多关系关联表)   # 用户ID外键   # 收藏新闻外键
Store = db.Table('Store',
                 db.Column('user_id', db.Integer, db.ForeignKey('user.user_id')),
                 db.Column('hash_id', db.String(64), db.ForeignKey('news.hash_id'))
                 )

class User(UserMixin, db.Model):
    __tablename__ = 'user'
    user_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    # 用户名
    username = db.Column(db.String(64), unique=True, nullable=False)
    # 密码
    password = db.Column(db.String(64), nullable=False)
    # 头像
    head_img = db.Column(db.String(128), nullable=False)
    # 邮箱
    email = db.Column(db.String(255), unique=True, nullable=False)
    # 登录状态
    is_login = db.Column(db.Boolean, default=0)
    # 账户激活状态
    is_active = db.Column(db.Boolean, default=0)
    # 收藏数
    store_num = db.Column(db.Integer, default=0)
    # 性别   # 1 男  2  女  0 隐藏性别
    sex = db.Column(db.Integer, default=2)
    # 生日
    birthday = db.Column(db.DateTime(), nullable=True)
    # 用户评论反向引用
    have_comment = db.relationship('Comment', uselist=True, lazy='dynamic')
    # 用户收藏反向引用
    have_store = db.relationship('News', uselist=True, lazy='dynamic',secondary=Store)

    def get_id(self):
        self.user_id = id
        return self.user_id


class Comment(db.Model):
    """
      评论表
    """
    __tablename__ = "comment"
    id = db.Column(db.Integer, primary_key=True)
    # 用户ID外键
    user_id = db.Column(db.Integer, db.ForeignKey(User.user_id), index=True)
    # 新闻ID外键
    hash_id = db.Column(db.String(64), db.ForeignKey(News.hash_id), index=True)
    # 评论内容
    content = db.Column(db.String(64), nullable=False)
    # 评论时间
    create_time = db.Column(db.DateTime, default=datetime.now)
    # 评论的父ID
    comment_obj = db.Column(db.Integer, default=0)
